package com.dems.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dems.dao.dataobject.RoomUserRelation;
import com.dems.dao.dataobject.User;
import com.dems.dao.mapper.la.RoomUserRelationMapper;
import com.dems.domain.RoomUserRelationReqVO;
import com.dems.service.RoomUserRelationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RoomUserRelationServiceImpl implements RoomUserRelationService {

    @Autowired
    private RoomUserRelationMapper relationMapper;

    @Override
    public Page<RoomUserRelation> findByPage(RoomUserRelationReqVO reqVO) {
        Page page = new Page(reqVO.getPageNum(), reqVO.getPageSize());
        RoomUserRelation userRelation = new RoomUserRelation();
        BeanUtils.copyProperties(reqVO, userRelation);
        return relationMapper.queryByPage(page, userRelation);
    }

    @Override
    public int saveRelation(List<RoomUserRelation> roomUserRelations, User user) {
        for(RoomUserRelation roomUserRelation : roomUserRelations) {
            roomUserRelation.setCreateUserId(user.getUserId());
            roomUserRelation.setCreateBy(user.getName());
            roomUserRelation.setCreateTime(new Date());
            relationMapper.insertSelective(roomUserRelation);
        }
        return 1;
    }

    @Override
    public int updateRelation(RoomUserRelation roomUserRelation, User user) {
        roomUserRelation.setUpdateBy(user.getName());
        roomUserRelation.setUpdateUserId(user.getUserId());
        roomUserRelation.setUpdateTime(new Date());
        return relationMapper.updateByPrimaryKeySelective(roomUserRelation);
    }
}
